<script setup lang="ts">
import type { Settings } from './types';

import { onMounted, ref } from 'vue';
import { $t } from '#/locales';
import { message, Skeleton } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { getSettingSite, putSettingSite } from '#/api/admin/setting';

import { defaultSettings, updateSettings } from './hook';

const loading = ref<boolean>(true);
const form = ref<Settings>(defaultSettings);
async function onSubmit(values: Record<string, any>) {
  const { msg } = await putSettingSite(updateSettings(form.value, values));
  message.success(msg);
}

const [Form, formApi] = useVbenForm({
  handleSubmit: onSubmit,
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'footer_address',
      label: $t("admin.Address"),
      rules: 'required',
    },
   
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'footer_tel',
      label: $t("admin.Address"),
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'footer_Fax',
      label: 'Fax',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'footer_email',
      label: $t("admin.Email"),
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'footer_iva',
      label: 'P.IVA',
    },   
    {
      component: 'Upload',
      componentProps: {
        placeholder: '二维码',
      },
      fieldName: 'scan',
      label: $t("admin.QR Code"),
    },   
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'ios_download',
      label: $t("admin.iOS Download URL"),
    },   
    {
      component: 'Input',
      componentProps: {
        placeholder: $t('page.common.Please Enter'),
      },
      fieldName: 'andriod_download',
      label: $t("admin.Android Download URL"),
    },   

    
  ],
});

const onInfo = async () => {
  const { data } = await getSettingSite();
  if (data.site_info) {
    form.value.options = data.site_info;
    formApi.setValues(data.site_info);
  }
  if (data.cdn_settings) {
    form.value.cdn_settings = data.cdn_settings;
  }

  if (data.cmf_settings) {
    form.value.cmf_settings = data.cmf_settings;
  }
  loading.value = false;
};





onMounted(async () => {
  await onInfo();
});
</script>


<template>
  <Skeleton :loading="loading">

      <Form/>

  </Skeleton>
</template> 

